<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>模拟</title>
  </head>
  <body>
    <script>
      /*
          模拟 map  every some
         */
      //map 映射
      const map = function (arr, fn) {
        let res = [];
        for (let val of arr) {
          res.push(fn(val));
        }
        return res;
      };
      let arr = [1, 2, 3, 4];
      //测试 求平方
      let m = map(arr, (item) => item * item);
      console.log(m);

      //every 判断所有元素是否指定条件
      const every = function (arr, fn) {
        let res = true;
        for (let val of arr) {
          res = fn(val);
          if (!res) {
            break;
          }
        }
        return res;
      };
      //判断数组内所有的数是否大于2
      let r = every(arr, (item) => item > 2);
      console.log(r);
      //every 匹配数组内有一个满足条件
      const some = function (arr, fn) {
        let res = false;
        for (let val of arr) {
          res = fn(val);
          if (res) {
            break;
          }
        }
        return res;
      };
      let s = every(arr, (item) => item % 2);
      console.log(s);
    </script>
  </body>
</html>
